import Vue from 'vue'
import VueRouter from 'vue-router'
import vuex from "@/store/index"

Vue.use(VueRouter)

const routes = [
  //首页
  {
    path: '/',
    name: 'home',
    component: () => import(/* webpackChunkName: "about" */ '../views/home/home.vue')
  },

  //人员清单1
  {
    path: '/personnelOne',
    name: 'personnelOne',
    component: () => import(/* webpackChunkName: "about" */ '../views/personnel/personnelOne/personnelOne.vue')
  },
  //人员清单2
  {
    path: '/personnelTwo',
    name: 'personnelTwo',
    component: () => import(/* webpackChunkName: "about" */ '../views/personnel/personnelTwo/personnelTwo.vue')
  },

  //寻仙大道争锋
  {
    path: '/gamesXxddzf',
    name: 'gamesXxddzf',
    component: () => import(/* webpackChunkName: "about" */ '../views/gemes/gamesXxddzf/gamesXxddzf.vue')
  },

  //聊天室1
  {
    path: '/chatRoom1',
    name: 'chatRoom1',
    component: () => import(/* webpackChunkName: "about" */ '../views/chatRoom/chatRoom1/chatRoom1.vue')
  },
  //聊天室2
  {
    path: '/chatRoom2',
    name: 'chatRoom2',
    component: () => import(/* webpackChunkName: "about" */ '../views/chatRoom/chatRoom2/chatRoom2.vue')
  },

  //会员信息
  {
    path: '/WLCakeShopMember',
    name: 'WLCakeShopMember',
    component: () => import(/* webpackChunkName: "about" */ '../views/WLCakeShop/WLCakeShopMember/WLCakeShopMember.vue')
  },
  //商品
  {
    path: '/WLCakeShopCommodity',
    name: 'WLCakeShopCommodity',
    component: () => import(/* webpackChunkName: "about" */ '../views/WLCakeShop/WLCakeShopCommodity/WLCakeShopCommodity.vue')
  },

  //配置中心
  {
    path: '/configure',
    name: 'configure',
    component: () => import(/* webpackChunkName: "about" */ '../views/configure/configure.vue')
  },

  //登录页面
  {
    path: '/signin',
    name: 'signin',
    component: () => import(/* webpackChunkName: "about" */ '../views/signin/signin.vue')
  },
  //error
  {
    path: '*',
    name: '*',
    component: () => import(/* webpackChunkName: "about" */ '../views/error/404.vue')
  },

  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  //获取cookie值
  let cookieData = ""
  var arrstr = document.cookie.split("; ");
  for (var i = 0; i < arrstr.length; i++) {
    var temp = arrstr[i].split("=");
    if (temp[0] == "username") cookieData = unescape(temp[1]);
  }
  // 守卫业务
  if (to.path == '/signin') {
    if (cookieData) {
      //如果已经登录 则跳转到首页
      vuex.username = cookieData
      next("/")
    } else {
      next(true)
    }
  } else {
    //是否登录的判断
    if (!cookieData) {
      next('/signin');
    } else {
      vuex.username = cookieData
      next(true);
    }
  }
})

export default router
